﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Corecalc;

namespace Sisal
{
    public class GRGraph
    {
        public GRNode Root { get; set; }
        public List<GRNode> Nodes { get; set; }

        public GRGraph(GRNode root, List<GRNode> nodes) {
            Root = root;
            Nodes = Procedures.TopSort(nodes, n => n.OutputEdges).ToList();

            foreach (GRNode formulaNode in Nodes) {
                formulaNode.Parent = this;
            }
        }

        public void PrintGraph() {
            Console.WriteLine("- - - - - - - - - - TP Sorted Nodes - - - - - - - - - - - - ");
            IEnumerable<GRNode> topSortedNodes = Procedures.TopSort(Nodes, n => n.OutputEdges);
            foreach (var node in topSortedNodes) {
                Console.WriteLine(node);
            }
            Console.WriteLine();

            Console.WriteLine("- - - - - - - - - - Nodes - - - - - - - - - - - -");
            foreach (GRNode node in Nodes) {
                Console.WriteLine("{0}", node);

                foreach (GRNode inpNode in node.InputEdges) {
                    Console.WriteLine("\t\t\tInput from:\t {0}", inpNode);
                }

                foreach (GRNode outNode in node.OutputEdges) {
                    Console.WriteLine("\t\t\tOutput to:\t {0}", outNode);
                }

                Console.WriteLine();
            }

            Console.WriteLine("- - - - - - - - - - FormulaNodes - - - - - - - - - - - -");
            var formulaNodes = Nodes.Where(x => x.IsFormulaNode);
            foreach (GRNode fn in formulaNodes) {
                Console.WriteLine("{0}[{1}, {2}]: {3}", fn.Fca.sheet, fn.Fca.ca.col, fn.Fca.ca.row, fn);
            }
        }
    }
}
